package org.gdstash.ui.table;

import org.gdstash.db.DBItem;
import org.gdstash.item.GDItem;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;

public class GDItemInfo implements Comparable<GDItemInfo> {
    public static final Color COLOR_GREEN;
    public static final Color COLOR_YELLOW;
    public static final Color COLOR_RED;
    public GDItem gdItem;
    public int scCount;
    public int hcCount;
    public GDItem scBlueprint;
    public GDItem hcBlueprint;

    private GDItemInfo(final GDItem item) {
        this.gdItem = item;
        this.scCount = 0;
        this.hcCount = 0;
        this.scBlueprint = null;
        this.hcBlueprint = null;
    }

    static {
        COLOR_GREEN = new Color(0, 72, 0);
        COLOR_YELLOW = new Color(128, 48, 0);
        COLOR_RED = new Color(72, 0, 0);
    }

    public static List<GDItemInfo> buildItemInfo(final List<GDItem> all, final List<GDItem> found) {
        if (all == null) {
            return null;
        }
        if (all.isEmpty()) {
            return null;
        }
        final List<GDItemInfo> infos = new LinkedList<GDItemInfo>();
        for (final GDItem item : all) {
            final GDItemInfo info = new GDItemInfo(item);
            infos.add(info);
            for (final GDItem item2 : found) {
                if (item.getItemID().equals(item2.getItemID())) {
                    if (item2.isHardcore()) {
                        ++info.hcCount;
                    } else {
                        ++info.scCount;
                    }
                }
            }
        }
        return infos;
    }

    public BufferedImage getFullImage() {
        if (this.gdItem == null) {
            return null;
        }
        final DBItem dbItem = this.gdItem.getDBItem();
        if (this.gdItem.getDBItem() == null) {
            return null;
        }
        BufferedImage img = dbItem.getImage();
        if (img == null) {
            return null;
        }
        final int MARGIN_HEIGHT = 6;
        final int BLUEPRINT_HEIGHT = 32;
        final int INFO_HEIGHT = 82;
        final int IMAGE_HEIGHT = 210;
        final int IMAGE_WIDTH = 100;
        final BufferedImage clone = new BufferedImage(100, 210, img.getType());
        final Graphics2D g = clone.createGraphics();
        int xPos = (100 - img.getWidth()) / 2;
        int yPos = (210 - img.getHeight() - 82) / 2;
        g.drawImage(img, xPos, yPos, null);
        img = clone;
        if (this.scCount > 0) {
            final String s = Integer.toString(this.scCount);
            Font font = UIManager.getDefaults().getFont("Label.font");
            font = font.deriveFont(1, 10.0f);
            g.setFont(font);
            final int h = g.getFontMetrics().getAscent();
            xPos = 6;
            yPos = 210 - h - 6 - 32 - 6;
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos + 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos + 1);
            g.setPaint(Color.WHITE);
            g.drawString(s, xPos, yPos);
        }
        if (this.scBlueprint != null) {
            final BufferedImage bpImg = this.scBlueprint.getImage();
            if (bpImg != null) {
                xPos = 100 - bpImg.getWidth() - 6;
                yPos = 210 - bpImg.getHeight() - 6 - 32 - 6;
                g.drawImage(bpImg, xPos, yPos, null);
            }
        }
        if (this.hcCount > 0) {
            final String s = Integer.toString(this.hcCount);
            Font font = UIManager.getDefaults().getFont("Label.font");
            font = font.deriveFont(1, 10.0f);
            g.setFont(font);
            final int w = g.getFontMetrics().stringWidth(s);
            final int h2 = g.getFontMetrics().getDescent();
            xPos = 6;
            yPos = 210 - h2 - 6;
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos + 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos + 1);
            g.setPaint(Color.WHITE);
            g.drawString(s, xPos, yPos);
        }
        if (this.hcBlueprint != null) {
            final BufferedImage bpImg = this.hcBlueprint.getImage();
            if (bpImg != null) {
                xPos = 100 - bpImg.getWidth() - 6;
                yPos = 210 - bpImg.getHeight() - 6;
                g.drawImage(bpImg, xPos, yPos, null);
            }
        }
        return img;
    }

    public BufferedImage getImage() {
        if (this.gdItem == null) {
            return null;
        }
        return this.gdItem.getImage();
    }

    @Override
    public int compareTo(final GDItemInfo info) {
        final int i = this.gdItem.compareTo(info.gdItem);
        if (i != 0) {
            return i;
        }
        if (this.scCount != info.scCount) {
            return this.scCount - info.scCount;
        }
        return this.hcCount - info.hcCount;
    }
}
